import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/',
    //name: 'layout', 有默认子路由，这个name无意义
    component: () => import('@/views/layout'),
    children:[
      {
        path: '', //path为空，默认子路由
        name: 'home',
        component: () => import('@/views/home')
      },
      {
        path: '/article', //内容管理
        name: 'article',
        component: () => import('@/views/article')
      }
    ]
  },
  
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to,from,next)=>{
  const token = window.localStorage.getItem("user")
  if(to.path!=='/login'){
    if(token){
      next()
    }else{
      next('/login')
    }
  }else{
    next()//放行
  }
})

export default router
